package com.larry.mr.compress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;
import org.junit.Test;

import java.io.*;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 21:23 2021/11/17
 * @Description TODO 解压缩测试
 **/
public class CompressTest {

    //输入输出路径
    String srcFile = "C:\\Users\\larry\\IdeaProjects\\hadoop\\MapReduce\\src\\main\\resources\\jianai\\ja.txt";
    String srcFile2 = "C:\\Users\\larry\\IdeaProjects\\hadoop\\MapReduce\\src\\main\\resources\\jianai\\ja.txt.deflate";
    String destFile = "C:\\Users\\larry\\IdeaProjects\\hadoop\\MapReduce\\src\\main\\resources\\jianai\\ja.txt";

    /**
     * 压缩
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @Test
    public void testCompress() throws IOException, ClassNotFoundException {

        //输入流
        FileInputStream fis = new FileInputStream(new File(srcFile));
        Configuration configuration = new Configuration();
        String classPath = "org.apache.hadoop.io.compress.DefaultCodec";
        Class<?> aClass = Class.forName(classPath);
        //获取压缩对象
        CompressionCodec compressionCodec = (CompressionCodec) ReflectionUtils.newInstance(aClass, configuration);
        //输出流
        FileOutputStream fos = new FileOutputStream(new File(destFile + compressionCodec.getDefaultExtension()));
        //包装输出流
        CompressionOutputStream outputStream = compressionCodec.createOutputStream(fos);
        //读写数据
        IOUtils.copyBytes(fis,fos,configuration);

        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }


    /**
     * 解压缩
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @Test
    public void testDeCompress() throws IOException, ClassNotFoundException {

        //输入流
        FileInputStream fis = new FileInputStream(new File(srcFile2));


        Configuration configuration = new Configuration();
        CompressionCodec codec = new CompressionCodecFactory(configuration).getCodec(new Path(srcFile2));
        CompressionInputStream inputStream = codec.createInputStream(fis);

        //输出流
        FileOutputStream fos = new FileOutputStream(new File(destFile));
        //读写数据
        IOUtils.copyBytes(inputStream,fos,configuration);

        IOUtils.closeStream(inputStream);
        IOUtils.closeStream(fos);
    }

}
